﻿using Bussiness.DEV;
using EZSockets;
using Model;
using System;
using System.Collections.Generic;

namespace Bussiness
{
    public class SANLING : IDEV
    {
        public Device Device { get; set; }
        public List<DevicePOI> DevicePOIList { get; set; }
        private Simlib _Simlib;
        public Result Connect()
        {
            _Simlib = new Simlib();
            Result result = _Simlib.GetSimConnect(Device.DevCode, Device.Port.ToString(), "100", Device.IP);
            return result;
        }

        public Result<List<Cfg_Field>> Read()
        {
            Result<List<Cfg_Field>> result = new Result<List<Cfg_Field>>();
            result.Data = new List<Cfg_Field>();
            Communicationcs<string, string> pos;
            _Simlib.GetAxisPosition(3, out pos);

            int plData;
            string loadStr;
            string alarmString = null;
            List<string> servoSpeedInfo = new List<string>();
            List<string> servoCurrentInfo = new List<string>();
            for (int i = 1; i <= 3; i++)
            {
                switch (i)
                {
                    case 1:
                        alarmString = "Monitor_GetServoMonitorSpeed";
                        break;
                    case 2:
                        alarmString = "Monitor_GetServoMonitorCurrent";
                        break;

                }
                _Simlib.GetServoInfo(i, 2, out plData, out loadStr, alarmString);
                servoSpeedInfo.Add(plData.ToString());
                _Simlib.GetServoInfo(i, 3, out plData, out loadStr, alarmString);
                servoCurrentInfo.Add(plData.ToString());
            }

            double pdspeed,pdspeedFM, pdspeedFS, pdspeedFE;
            List<string> info = new List<string>();
            _Simlib.GetSpindleInfo(2, 1, out plData, out loadStr);//转速
            info.Add(plData.ToString());
            _Simlib.GetSpindleInfo(3, 1, out plData, out loadStr);//负载
            info.Add(plData.ToString());
            _Simlib.GetSpindleInfo(4, 1, out plData, out loadStr);//LED
            info.Add(plData.ToString());
            _Simlib.GetSpindleInfo(5, 1, out plData, out loadStr);//报警
            info.Add(plData.ToString());
            _Simlib.GetFeedSpeed(0, out pdspeed, "GetFeedSpeedFA"); //获取指令进给速度
            _Simlib.GetFeedSpeed(1, out pdspeedFM, "GetFeedSpeedFM");//手动有效进给速度（FM）
            _Simlib.GetFeedSpeed(2, out pdspeedFS, "GetFeedSpeedFS");//同步进给速度（FS）;
            _Simlib.GetFeedSpeed(3, out pdspeedFE, "GetFeedSpeedFE");//攻丝（FE）;

            foreach (var devicePOI in DevicePOIList)
            {
                Cfg_Field cfg_Field = new Cfg_Field();
                cfg_Field.name = devicePOI.DevPOIAlias;
                bool has = true;
                switch (devicePOI.DevPOIName)
                {
                    case "机械坐标X":
                        cfg_Field.value = pos.simMaPos[0];
                        break;
                    case "机械坐标Y":
                        cfg_Field.value = pos.simMaPos[1];
                        break;
                    case "机械坐标Z":
                        cfg_Field.value = pos.simMaPos[2];
                        break;
                    case "工件坐标X":
                        cfg_Field.value = pos.simWorkPos[0];
                        break;
                    case "工件坐标Y":
                        cfg_Field.value = pos.simWorkPos[1];
                        break;
                    case "工件坐标Z":
                        cfg_Field.value = pos.simWorkPos[2];
                        break;
                    case "相对坐标X":
                        cfg_Field.value = pos.simCurPos[0];
                        break;
                    case "相对坐标Y":
                        cfg_Field.value = pos.simCurPos[1];
                        break;
                    case "相对坐标Z":
                        cfg_Field.value = pos.simCurPos[2];
                        break;
                    case "剩余移动量X":
                        cfg_Field.value = pos.simDisPos[0];
                        break;
                    case "剩余移动量Y":
                        cfg_Field.value = pos.simDisPos[1];
                        break;
                    case "剩余移动量Z":
                        cfg_Field.value = pos.simDisPos[2];
                        break;
                    case "伺服速度X":
                        cfg_Field.value = servoSpeedInfo[0];
                        break;
                    case "伺服速度Y":
                        cfg_Field.value = servoSpeedInfo[1];
                        break;
                    case "伺服速度Z":
                        cfg_Field.value = servoSpeedInfo[2];
                        break;
                    case "伺服负载X":
                        cfg_Field.value = servoCurrentInfo[0];
                        break;
                    case "伺服负载Y":
                        cfg_Field.value = servoCurrentInfo[1];
                        break;
                    case "伺服负载Z":
                        cfg_Field.value = servoCurrentInfo[2];
                        break;
                    case "伺服倍率X":
                        cfg_Field.value = (Convert.ToInt32(servoSpeedInfo[0]) / pdspeed).ToString();
                        break;
                    case "伺服倍率Y":
                        cfg_Field.value = (Convert.ToInt32(servoSpeedInfo[1]) / pdspeed).ToString();
                        break;
                    case "伺服倍率Z":
                        cfg_Field.value = (Convert.ToInt32(servoSpeedInfo[2]) / pdspeed).ToString();
                        break;
                    case "主轴负载":
                        cfg_Field.value = info[0];
                        break;
                    case "实际主轴转速":
                        cfg_Field.value = info[1];
                        break;
                    case "主轴倍率":
                        cfg_Field.value = pdspeed==0?string.Empty:(Convert.ToInt32(info[1]) / pdspeed).ToString();
                        break;
                    case "主轴报警":
                        cfg_Field.value = info[3];
                        break;
                    case "进给速度FA":
                        cfg_Field.value = pdspeed.ToString();
                        break;
                    case "进给速度FM":
                        cfg_Field.value = pdspeedFM.ToString();
                        break;
                    case "进给速度FS":
                        cfg_Field.value = pdspeedFS.ToString();
                        break;
                    case "进给速度FE":
                        cfg_Field.value = pdspeedFE.ToString();
                        break;
                    case "单次加工时间": 
                        break;
                    case "产量计数":
                        string pvValue;
                        _Simlib.GetParaValue(30, 8002, 1, 1, out pvValue);
                        cfg_Field.value = pvValue;
                        break;
                    case "设备运行状态":
                        cfg_Field.value=GetRunStatus();
                        break;
                    default:
                        has = false;
                        break;
                }

                if (has)
                {
                    result.Data.Add(cfg_Field);
                }
            }

            return result;
        }

        /// <summary>
        /// 获取运行状态
        /// </summary>
        string GetRunStatus()
        {
            List<string> status = new List<string>();
            int plstatus;
            for (int i = 0; i < 4; i++)
            {
                _Simlib.GetRunStatus(i, out plstatus);
                switch (i)
                {
                    case 0:
                        if (plstatus == 0)
                        {
                            status.Add("刀具未测量");
                        }
                        else
                        {
                            status.Add("刀具已测量");
                        }

                        break;
                    case 1:
                        if (plstatus == 0)
                        {
                            status.Add("未自动运行");
                        }
                        else
                        {
                            status.Add("自动运行");
                        }

                        break;
                    case 2:
                        if (plstatus == 0)
                        {
                            status.Add("未开始自动运行");
                        }
                        else
                        {
                            status.Add("开始自动运行");
                        }

                        break;
                    case 3:
                        if (plstatus == 0)
                        {
                            status.Add("自动运行未暂停");
                        }
                        else
                        {
                            status.Add("自动运行暂停");
                        }

                        break;
                }
            }

            return status[0] + "/" + status[1] + "/" + status[2] +"/"+ status[2];
        }
    }
}
